package com.xxx.baseapplication.fragment

import android.content.Context
import android.os.Bundle
import android.widget.ImageView
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import cn.bingoogolapple.bgabanner.BGABanner
import com.xxx.baseapplication.R
import com.xxx.baseapplication.adapter.ArticleAdapter
import com.xxx.baseapplication.databinding.FragmentArticleBinding
import com.xxx.baseapplication.vm.ArticleVM
import com.xxx.baseapplication.vo.BannerVO
import com.xxx.mylibrary.base.BaseFragment
import com.xxx.mylibrary.binds.displayWithUrl
import com.xxx.mylibrary.binds.screenHeight
import com.xxx.mylibrary.binds.viewModel

/**
 * 文章列表1
 */
class ArticleFragment : BaseFragment<FragmentArticleBinding>() {
    override val layoutId: Int
        get() = R.layout.fragment_article
    val vm by viewModel<ArticleVM>()
    private val adapter = ArticleAdapter()

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        initRecyclerView()
        initBanner()
        binding.vm = vm
        binding.executePendingBindings()
        binding.refreshLayout.autoRefresh()
    }

    private fun initBanner() {
        binding.banner.run {
            val adapter: BGABanner.Adapter<ImageView, BannerVO> =
                BGABanner.Adapter { _, image,
                                    model,
                                    _ ->
                    image.displayWithUrl(model?.imagePath)
                }
            setAdapter(adapter)
            setDelegate { _, _, model, _ ->
                if (model is BannerVO) {
                }
            }
        }
        binding.banner
        //登录状态变化
        context?.screenHeight()
        var context: Context? = null
        context?.screenHeight()
    }

    private fun initRecyclerView() {
        binding.recyclerView.also {
            it.adapter = adapter
            it.layoutManager = LinearLayoutManager(activity)
        }

        vm._articlePage.observe(viewLifecycleOwner, Observer {
            adapter.addAll(it.datas, it.curPage == 1)
        })
    }

}